//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    whichData: [{
      id: "4",
      value: "价格虚高",
      active: false
    },
    {
      id: "5",
      value: "产品假劣",
      active: false
    },
    {
      id: "6",
      value: "态度恶劣",
      active: false
    },
    ],
    id: "",
    content: "",
    // 
    uid: "",
    userType: "",
    businessId: 0,
    // 顶部图
    applyfor: [],
    // 图片
    imgUrl: baseJs.imgUrl,
    imgList: [],
    imgStr: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      businessId: options.businessId,
    })

    this.GetServerImg();
  },
  // 获取网络图片
  GetServerImg: function () {
    var that = this;
    var applyfor = this.data.applyfor;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetServerImg"),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        var applyfor = json.ResultData.tousu;
        console.log(json);
        that.setData({
          applyfor: applyfor,
        })
        // console.log(applyfor);
      }
    })
  },
  // 点击图片跳转
  bindImg: function () {
    wx.navigateTo({
      url: '/packageA/touSu/touSu',
    })
  },
  //选择反馈类型
  choose: function (e) {
    // console.log(e);
    var that = this;
    var id = this.data.id;
    var whichData = this.data.whichData;
    var value = e.currentTarget.dataset.value;
    for (var i = 0, len = whichData.length; i < len; ++i) {
      if (whichData[i].value == value) {
        whichData[i].active = true;
      } else {
        whichData[i].active = false
      }
    }
    this.setData({
      whichData: whichData,
      id: e.currentTarget.dataset.id,
      value: value
    })
  },
  //意见内容获取
  textAreaBlur: function (e) {
    // console.log(e);
    this.setData({
      content: e.detail.value
    })
  },
  // 提交反馈
  handUp: function () {
    var that = this;
    var uid = this.data.uid;
    var id = this.data.id;
    var content = this.data.content;
    var imgList = this.data.imgList;
    var imgStr = this.data.imgStr;
    imgStr = imgList.join(';');
    // console.log(id);
    if (id == "") {
      wx.showToast({
        title: "请选择反馈类型",
        icon: 'none',
        duration: 1500,
      })
      return false;
    }
    if (content == "") {
      wx.showToast({
        title: "请输入反馈内容",
        icon: 'none',
        duration: 1500,
      })
      return false;
    }
    console.log("method=AddFeedBackInfo&UID=" + uid + "&Content=" + content + "&Type=" + id + "&Images=" + imgStr);
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AddFeedBackInfo&UID=" + uid + "&Content=" + content + "&Type=" + id + "&Images=" + imgStr),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1500,
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)

        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1500,
          })
        }
      }
    })

  },
  // delTap
  delTap: function (e) {
    var imgList = this.data.imgList;
    imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: imgList,
    })
  },
  // 选择头像
  chooseImg: function (e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var imgList = this.data.imgList;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res);
        console.log(imgList);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        // base64 = wx.arrayBufferToBase64(res.data);
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            // console.log('data:image/png;base64,' + res.data)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: res.data,
                type: 14,
              }),
              success: function (res) {
                console.log(res);
                imgList.push(imgUrl + res.data.ResultData);
                that.setData({
                  imgList: imgList,

                })
              }
            })
          }
        })
      }
    })
  },
})